<?php
namespace app\admin\logic;

use app\api\cache\TokenCache;
use app\common\logic\AccountLogLogic;
use app\common\model\AccountLog;
use app\common\model\Order;
use app\common\model\Pay;
use app\common\model\User;
use app\common\server\UrlServer;
use think\Db;
use think\Exception;

class UserLogic
{
    /**
     * 列表
     * @param $get
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function lists($get){
        $user = new User();

        $where = [];
        $where[] = ['delete_time','=', '0'];

        //查询
        if(isset($get['keyword']) && $get['keyword']){
            $where[] = [$get['keyword_type'],'like','%'.$get['keyword'].'%'];
        }

        //消费金额
        if(isset($get['total_amount_start']) && $get['total_amount_start']){
            $where[] = ['total_order_amount','>=',$get['total_amount_start']];
        }
        if(isset($get['total_amount_end']) && $get['total_amount_end']){
            $where[] = ['total_order_amount','<=',$get['total_amount_end']];
        }

        //注册时间
        if(isset($get['start_time']) && $get['start_time']!=''){
            $where[] = ['create_time','>=',strtotime($get['start_time'])];
        }
        if(isset($get['end_time']) && $get['end_time']!=''){
            $where[] = ['create_time','<=',strtotime($get['end_time'])];
        }

        $user_count = $user
                    ->where($where)
                    ->count();

        $user_list = $user
                    ->where($where)
                    ->field('password,pay_password,salt',true)
                    ->page($get['page'],$get['limit'])
                    ->order('id desc')
                    ->select()->toArray();
        

        $all_user_list = $user->where(['delete_time'=>0])->column('sn,nickname,avatar,mobile','id');

        foreach ($user_list as &$item){
            $item['total_amount'] = round($item['user_money'] + $item['earnings'], 2);
            $item['group_name'] = '-';


            if ($item['avatar'] != '/static/common/image/default/user.png') {
                $item['abs_avatar'] = $item['avatar'] ? UrlServer::getFileUrl($item['avatar']) : '';
            } else {
                $item['abs_avatar'] = '/static/common/image/default/user.png';
            }


           

        }

        return ['count'=>$user_count , 'lists'=>$user_list];
    }

    public static function exportFile($get)
    {
        $where[] = ['del','=', '0'];

        //查询
        if(isset($get['keyword']) && $get['keyword']){
            $where[] = [$get['keyword_type'],'like','%'.$get['keyword'].'%'];
        }
        //消费金额
        if(isset($get['total_amount_start']) && $get['total_amount_start']){
            $where[] = ['total_order_amount','>=',$get['total_amount_start']];
        }
        if(isset($get['total_amount_end']) && $get['total_amount_end']){
            $where[] = ['total_order_amount','<=',$get['total_amount_end']];
        }

        //注册时间
        if(isset($get['start_time']) && $get['start_time']!=''){
            $where[] = ['create_time','>=',strtotime($get['start_time'])];
        }
        if(isset($get['end_time']) && $get['end_time']!=''){
            $where[] = ['create_time','<=',strtotime($get['end_time'])];
        }

        $user_list = Db::name('user')->where($where)
            ->order('id desc')
            ->select();

       
        // 所有会员信息
        $all_user_list = User::where(['del'=>0])->column('sn,nickname,mobile','id');

        $exportData = [];
        foreach ($user_list as $item){
            
            $loginTime = date('Y-m-d H:i:s', $item['login_time']);

            $exportData[] = [$item['sn'], $item['nickname'], $item['total_order_amount'], $item['user_money'], $item['user_integral'], $loginTime];
        }

        $exportTitle = ['会员编号', '会员昵称', '消费金额', '账户余额', '积分', '最后登录时间'];
        $exportExt = 'xls';
        return ['exportTitle'=> $exportTitle, 'exportData' => $exportData, 'exportExt'=>$exportExt, 'exportName'=>'会员列表'.date('Y-m-d H:i:s')];
    }

    /**
     * Notes: 获取会员信息
     * @param $id
     * @param bool $getdata 是否获取原始数据
     * @param bool $expenditure 是否显示会员消费
     * @return User|mixed
     */
    public static function getUser($id,$getdata = false,$expenditure = false){
        $user =  User::get($id);
        if($getdata){
            $user = $user->getData();
            if($user['birthday']) $user['birthday'] = date('Y-m-d',$user['birthday']);
        }
        $user['abs_avatar'] = UrlServer::getFileUrl($user['avatar']);
        if($expenditure){
            //本月的消费金额
            $month_amount = Db::name('order')
                        ->where('order_status','not in',[Order::STATUS_CLOSE,Order::STATUS_WAIT_PAY])
                        ->where(['user_id'=>$id,'pay_status'=>Pay::ISPAID])
                        ->whereTime('create_time', 'month')
                        ->sum('order_amount');
            //本月的订单笔数
            $month_num = Db::name('order')
                       ->where('order_status','not in',[Order::STATUS_CLOSE,Order::STATUS_WAIT_PAY])
                       ->where(['user_id'=>$id,'pay_status'=>Pay::ISPAID])
                       ->whereTime('create_time', 'month')
                       ->count();
            //累计消费笔数
            $total_num =Db::name('order')
                        ->where('order_status','not in',[Order::STATUS_CLOSE,Order::STATUS_WAIT_PAY])
                        ->where(['user_id'=>$id,'pay_status'=>Pay::ISPAID])
                        ->sum('order_amount');
            $avg_amount = Db::name('order')
                        ->where('order_status','not in',[Order::STATUS_CLOSE,Order::STATUS_WAIT_PAY])
                        ->where(['user_id'=>$id,'pay_status'=>Pay::ISPAID])
                        ->avg('order_amount');
            $user['month_amount'] = '￥'.round($month_amount,2);
            $user['month_num'] = round($month_num,2);
            $user['total_num'] = round($total_num,2);
            $user['avg_amount'] = '￥'.round($avg_amount,2);
            // 头像
            $user['avatar'] = UrlServer::getFileUrl($user['avatar']);
        }
        return $user;
    }
    /*
     * 批量设置会员分组
     */
    

    public static function  fans($params)
    {
        $where = [];
        // 一级
        if ($params['type'] == 'one') {
            $where[] = ['first_leader', '=', $params['id']];
        }
        // 二级粉丝
        if ($params['type'] == 'two') {
            $where[] = ['second_leader', '=', $params['id']];
        }
        if(isset($params['keyword']) && !empty($params['keyword'])) {
            $where[] = [$params['field'], 'like', '%'. $params['keyword'] . '%'];
        }

        $lists = User::field('id,sn,nickname,avatar,first_leader')
            ->where($where)
            ->page($params['page'], $params['limit'])
            ->select()
            ->toArray();

        $count = User::field('id,sn,nickname,avatar,first_leader')
            ->where($where)
            ->count();

        foreach($lists as &$item) {
            $item['avatar'] = empty($item['avatar']) ? '' : UrlServer::getFileUrl($item['avatar']);
            $item['first_leader_info'] = User::getUserInfo($item['first_leader']);
            $item['fans'] = User::where([
                ['first_leader|second_leader', '=', $item['id']],
                ['delete_time', '=', 0]
            ])->count();
        }
        return [
            'lists' => $lists,
            'count' => $count,
        ];
    }

    /*
     * 用户账户调整
     */
    public static function adjustAccount($post_data){
        Db::startTrans();
        try {
            $update_data = [];
            $account_log = [];
            //余额调整
            if ($post_data['type'] == 'money') {
                $number = $post_data['money'];
                $change_type = 1;
                $source_type = AccountLog::admin_add_money;
                $money_sql = Db::raw('user_money + ' . $post_data['money']);
                if ($post_data['money_handle'] == 0) {
                    $change_type = 2;
                    $source_type = AccountLog::admin_reduce_money;
                    $money_sql = Db::raw('user_money - ' . $post_data['money']);
                }
                $update_data['user_money'] = $money_sql;
                $account_log[] = [
                    'number'        => $number,
                    'change_type'   => $change_type,
                    'source_type'   => $source_type,
                    'remark'        => $post_data['money_remark'],
                ];
            }
            //积分调整
            if ($post_data['type'] == 'integral') {
                $number = $post_data['integral'];
                $change_type = 1;
                $source_type = AccountLog::admin_add_integral;
                $integral_sql = Db::raw('user_integral + ' . $post_data['integral']);

                if ($post_data['integral_handle'] == 0) {
                    $change_type = 2;
                    $source_type = AccountLog::admin_reduce_integral;
                    $integral_sql = Db::raw('user_integral - ' . $post_data['integral']);
                }
                $update_data['user_integral'] = $integral_sql;
                $account_log[] = [
                    'number'        => $number,
                    'change_type'   => $change_type,
                    'source_type'   => $source_type,
                    'remark'        => $post_data['integral_remark'],
                ];
            }
            //成长值调整
            if ($post_data['type'] == 'growth') {
                $number = $post_data['growth'];
                $change_type = 1;
                $source_type = AccountLog::admin_add_growth;
                $growth_sql = Db::raw('user_growth + ' . $post_data['growth']);

                if ($post_data['growth_handle'] == 0) {
                    $change_type = 2;
                    $source_type = AccountLog::admin_reduce_growth;
                    $growth_sql = Db::raw('user_growth - ' . $post_data['growth']);
                }
                $update_data['user_growth'] = $growth_sql;
                $account_log[] = [
                    'number'        => $number,
                    'change_type'   => $change_type,
                    'source_type'   => $source_type,
                    'remark'        => $post_data['growth_remark'],
                ];
            }

            //佣金调整
            if ($post_data['type'] == 'earnings') {
                $number = $post_data['earnings'];
                $change_type = 1;
                $source_type = AccountLog::admin_inc_earnings;
                $money_sql = Db::raw('earnings + ' . $post_data['earnings']);
                if ($post_data['earnings_handle'] == 0) {
                    $change_type = 2;
                    $source_type = AccountLog::admin_reduce_earnings;
                    $money_sql = Db::raw('earnings - ' . $post_data['earnings']);
                }
                $update_data['earnings'] = $money_sql;
                $account_log[] = [
                    'number'        => $number,
                    'change_type'   => $change_type,
                    'source_type'   => $source_type,
                    'remark'        => $post_data['earnings_remark'],
                ];
            }

            Db::name('user')->where('id', $post_data['id'])->update($update_data);
            foreach ($account_log as $item => $value){
                AccountLogLogic::AccountRecord($post_data['id'],$value['number'],$value['change_type'],$value['source_type'],$value['remark']);
            }
            Db::commit();
            return true;
        } catch (Exception $e) {
            Db::rollback();
            return false;
        }
    }




    public static function create_password($password, $salt) {
        
        ///todo 生成密码
        return md5(md5($password) . $salt);
    }

    /*
     * 编辑会员
     */
    public static function edit($post){
        $user = User::get($post['id']);
        if (!empty($post['password'])) {
            //生成密码
            $post['password'] = self::create_password($post['password'], $user['salt']);
        } else {
            unset($post['password']);
        }
        $post['update_time'] = time();
        $post['birthday'] = strtotime($post['birthday']);
        return $user->allowField(true)->save($post);
    }

    public static function getList($get){
        switch ($get['type']){
            case 0://下单明细
                $order = new Order();
                $count = $order->alias('o')
                        ->where(['user_id'=>$get['user_id']])
                        ->where('order_status','not in',[Order::STATUS_CLOSE,Order::STATUS_WAIT_PAY])
                        ->count();

                $list = $order->alias('o')
                        ->where(['user_id'=>$get['user_id']])
                        ->where('order_status','not in',[Order::STATUS_CLOSE,Order::STATUS_WAIT_PAY])
                        ->page($get['page'],$get['limit'])
                        ->order('id desc')
                        ->field('o.id,order_sn,total_num,pay_time,total_amount,order_amount,discount_amount,
                                o.create_time,consignee,province,city,district,o.address,mobile')
                        ->select();

                break;
            case 1://积分明细
                $account_log =new AccountLog();
                $count = $account_log
                        ->where(['user_id'=>$get['user_id'],'source_type'=>AccountLog::integral_change])
                        ->count();
                $list = $account_log
                        ->where(['user_id'=>$get['user_id'],'source_type'=>AccountLog::integral_change])
                        ->page($get['page'],$get['limit'])
                        ->order('id desc')
                        ->select();
                break;
            case 2://签到明细
                $account_log =new AccountLog();
                $count = $account_log
                        ->where(['user_id'=>$get['user_id'],'source_type'=>AccountLog::sign_in_integral])
                        ->count();
                $list = $account_log
                        ->where(['user_id'=>$get['user_id'],'source_type'=>AccountLog::sign_in_integral])
                        ->page($get['page'],$get['limit'])
                        ->order('id desc')
                        ->select();
                break;
            case 3://余额明细
                $account_log = new AccountLog();
                $count = $account_log
                    ->where(['user_id'=>$get['user_id'],'source_type'=>AccountLog::money_change])
                    ->count();
                $list = $account_log
                    ->where(['user_id'=>$get['user_id'],'source_type'=>AccountLog::money_change])
                    ->page($get['page'],$get['limit'])
                    ->order('id desc')
                    ->select();
                break;
            case 4://推广明细
                
                $list = [];

                break;
            case 5://收货地址
                $count = Db::name('user_address')
                            ->where(['user_id'=>$get['user_id'],'del'=>0])
                            ->count();
                $list = Db::name('user_address')
                            ->where(['user_id'=>$get['user_id'],'del'=>0])
                            ->order('id desc')
                            ->select();
                foreach ($list as &$item){
                    $item['default'] = '否';
                    $region = Db::name('dev_region')
                        ->where('id', 'IN', [$item['province_id'], $item['city_id'], $item['district_id']])->order('level asc')->column('name');
                    $region_desc = implode('', $region);
                    $item['address'] = $region_desc.$item['address'];
                    if($item['is_default']){
                        $item['default'] = '是';
                    }
                }
                break;
        }

        return ['count'=>$count , 'lists'=>$list];
    }



    public static function userLists($params)
    {
        $where[] = ['delete_time', '=', 0];
        // 用户信息
        if (isset($params['keyword']) && !empty($params['keyword'])) {
            $where[] = ['sn|nickname', 'like', '%'. $params['keyword'] .'%'];
        }

        $lists = \app\common\model\User::field('id,sn,nickname,id as distribution')
            ->where($where)
            ->page($params['page'], $params['limit'])
            ->select()
            ->toArray();
        $count = User::where($where)->count();

        return [
            'count' => $count,
            'lists' => $lists,
        ];
    }

    public static function adjustFirstLeader($params)
    {
        Db::startTrans();
        try {
            switch($params['type']) {
                // 指定推荐人
                case 'assign':
                    $formatData = self::assignFirstLeader($params);
                    break;
                // 设置推荐人为系统,即清空上级
                case 'system':
                    $formatData = self::clearFirstLeader($params);
                    break;
            }

            $user = User::findOrEmpty($params['id']);
            // 旧关系链
            if (!empty($user->ancestor_relation)) {
                $old_ancestor_relation = $user->id . ',' .$user->ancestor_relation;
            } else {
                $old_ancestor_relation = $user->id;
            }

            // 更新当前用户的分销关系
            User::where(['id' => $params['id']])->update($formatData);

            //更新当前用户下级的分销关系
            $data = [
                'second_leader' => $formatData['first_leader'],
                'third_leader' => $formatData['second_leader'],
                'update_time'  => time()
            ];
            User::where(['first_leader' => $params['id']])->update($data);

            //更新当前用户下下级的分销关系
            $data = [
                'third_leader' => $formatData['first_leader'],
                'update_time'  => time()
            ];
            User::where(['second_leader' => $params['id']])->update($data);

            //更新当前用户所有后代的关系链
            Db::name('user')
                ->where("find_in_set({$params['id']},ancestor_relation)")
                ->exp('ancestor_relation', "replace(ancestor_relation,'{$old_ancestor_relation}','" . trim("{$params['id']},{$formatData['ancestor_relation']}", ',') . "')")
                ->update();

            Db::commit();
            return true;
        } catch(\Exception $e) {
            Db::rollback();
            self::$error = $e->getMessage();
            return false;
        }
    }

    public static function assignFirstLeader($params)
    {
        if (empty($params['first_id'])) {
            throw new \think\Exception('请选择推荐人');
        }
        $firstLeader = User::field(['id', 'first_leader', 'second_leader', 'third_leader', 'ancestor_relation'])
            ->where('id', $params['first_id'])
            ->findOrEmpty()
            ->toArray();
        if(empty($firstLeader)) {
            throw new \think\Exception('推荐人不存在');
        }
        if ($params['first_id'] == $params['id']) {
            throw new \think\Exception('不能指定上级是自己');
        }
        $ancestorArr = explode(',', trim($firstLeader['ancestor_relation']));
        if(!empty($ancestorArr) && in_array($params['id'], $ancestorArr)) {
            throw new \think\Exception('不能指定推荐人为自己的下级');
        }

        // 上级
        $first_leader_id = $firstLeader['id'];
        // 上上级
        $second_leader_id = $firstLeader['first_leader'];
        // 上上上级
        $third_leader_id = $firstLeader['second_leader'];
        // 拼接关系链
        $firstLeader['ancestor_relation'] = $firstLeader['ancestor_relation'] ?? ''; // 清空null值及0
        $my_ancestor_relation = $first_leader_id. ',' . $firstLeader['ancestor_relation'];
        // 去除两端逗号
        $my_ancestor_relation = trim($my_ancestor_relation, ',');
        $data = [
            'first_leader' => $first_leader_id,
            'second_leader' => $second_leader_id,
            'third_leader' => $third_leader_id,
            'ancestor_relation' => $my_ancestor_relation,
            'update_time'  => time()
        ];
        return $data;
    }

    public static function clearFirstLeader($params)
    {
        $data = [
            'first_leader' => 0,
            'second_leader' => 0,
            'third_leader' => 0,
            'ancestor_relation' => '',
            'update_time'  => time()
        ];
        return $data;
    }

    public static function getInfo($id)
    {
        $user =  User::field('id,sn,nickname,avatar,birthday,sex,mobile,create_time,login_time,user_money,user_growth,earnings,user_integral,total_order_integral,earnings_integral')
            ->findOrEmpty($id);
        if($user->isEmpty()) {
            return [];
        }

        $user =$user->toArray();
        $orderWhere = [
            'user_id' => $id,
            'delete_time' => 0,
            'pay_status' => 1
        ];
        // 总资产
        $user['assets'] = $user['user_money'] + $user['earnings'];
        // 总订单数
        $user['order_num'] = Order::where($orderWhere)->count();
        // 总消费金额
        $user['total_amount'] = Order::where($orderWhere)->sum('order_amount');
        $user['total_amount'] = round($user['total_amount'] ,2);
        // 平均消费单价
        $user['avg_amount'] = Order::where($orderWhere)->avg('order_amount');
        $user['avg_amount'] = round($user['avg_amount'], 2);
        // 头像
        $user['avatar'] = UrlServer::getFileUrl($user['avatar']);
        // 用户积分余额
        $user['user_integral'] = $user['user_integral'];
        // 用户累计消费积分
        $user['total_order_integral'] = $user['total_order_integral'];
        // 用户累计赚取积分
        $user['earnings_integral'] = $user['earnings_integral'];
        return $user;
    }

    
}